home *** CD-ROM | disk | FTP | other *** search
- //////////////////////////////////////////////////////////////////////
- // Pocket PC Game Programming
- // Chapter 15: Infrared and Socket Communications
- //
- // Project02.cpp - Socket Test
- //
- //
- //////////////////////////////////////////////////////////////////////
-
- #include "stdafx.h"
- #include "Project02.h"
-
- //////////////////////////////////////////////////////////////////////
- // GameInit
- // Triggered by WinMain before window is created
- //////////////////////////////////////////////////////////////////////
- BOOL GameInit(HINSTANCE hInst)
- {
- //create new game library objecti
- Game = new CGameLibrary(hInst, _T("Project02"));
- if (Game == NULL)
- return FALSE;
-
- //set up the game window for creation
- Game->SetFullscreen(TRUE);
- Game->G_SetDisplay(FALSE);
- Game->SetTitle(_T("Socket Test"));
- Game->SetFrameRate(FRAME_RATE);
-
- return TRUE;
- }
-
- //////////////////////////////////////////////////////////////////////
- // GameStart
- // Triggered by WndProc after game window is activated
- //////////////////////////////////////////////////////////////////////
- void GameStart(HWND hWnd)
- {
- // seed the random number generator
- srand(GetTickCount());
-
- //create double buffer
- cbDoubleBuffer = new CBitmap(GetDC(hWnd));
- if (!cbDoubleBuffer->Create(Game->ScreenWidth(), Game->ScreenHeight()))
- Game->FatalError(L"Error creating double buffer");
-
- //use the double buffer
- g_hdc = cbDoubleBuffer->GetSourceDC();
-
- //load background image
- cbBackground = new CBitmap(g_hdc);
- if (!cbBackground->Load(Game->GetPath(L"socket.bmp")))
- Game->FatalError(L"Error loading socket.bmp");
-
- //draw background image to double buffer
- cbBackground->BitBlit(0, 0);
-
- if (SERVER_MODE)
- {
- hServerThread = CreateThread(NULL, 0, ServerThread, 0, 0, &dwThreadID);
- if (hServerThread == NULL)
- {
- Game->FatalError(L"Error creating server thread!");
- }
- }
- else
- {
- hClientThread = CreateThread(NULL, 0, ClientThread, 0, 0, &dwThreadID);
- if (hClientThread == NULL)
- {
- Game->FatalError(L"Error creating client thread!");
- }
- }
- }
-
- //////////////////////////////////////////////////////////////////////
- // GameActivate
- // Triggered by WndProc when game is activated
- //////////////////////////////////////////////////////////////////////
- void GameActivate(HWND hWnd)
- {
- }
-
- //////////////////////////////////////////////////////////////////////
- // GamePaint
- // Window repaint event (usually triggered once at startup)
- //////////////////////////////////////////////////////////////////////
- void GamePaint(HWND hWnd)
- {
- HDC hdc;
- PAINTSTRUCT ps;
-
- hdc = BeginPaint(hWnd, &ps);
-
- //draw double buffer to screen
- BitBlt(hdc, 0, 0, Game->ScreenWidth(), Game->ScreenHeight(),
- cbDoubleBuffer->GetSourceDC(), 0, 0, SRCCOPY);
-
- EndPaint(hWnd, &ps);
- }
-
- //////////////////////////////////////////////////////////////////////
- // GameEvent
- // Triggered by the message loop in WinMain
- //////////////////////////////////////////////////////////////////////
- void GameEvent()
- {
- HWND hWnd;
- HDC hdc;
-
- hWnd = Game->GetWindow();
- hdc = GetDC(hWnd);
-
- BitBlt(cbDoubleBuffer->GetSourceDC(), 10, 100, 200, 80,
- cbBackground->GetSourceDC(), 10, 100, SRCCOPY);
-
- xPos = 4;
- line = 6;
-
- if (SERVER_MODE)
- wsprintf(szMessage, L"SERVER ACTIVATED");
- else
- wsprintf(szMessage, L"CLIENT ACTIVATED");
-
- Game->PrintText(g_hdc, szMessage, xPos, line++, clrWhite, TRANSPARENT);
- line++;
-
- wsprintf(szMessage, L"Status: %s", szSocketStatus);
- Game->PrintText(g_hdc, szMessage, xPos, line++, clrWhite, TRANSPARENT);
- line++;
-
- wsprintf(szMessage, L"Message: %s", szIncomingMessage);
- Game->PrintText(g_hdc, szMessage, xPos, line++, clrWhite, TRANSPARENT);
- line++;
-
- wsprintf(szMessage, L"Bytes: %d", bytesReceived);
- Game->PrintText(g_hdc, szMessage, xPos, line++, clrWhite, TRANSPARENT);
-
- wcscpy(szIncomingMessage, _T(""));
-
- //draw double buffer to screen
- BitBlt(hdc, 0, 0, Game->ScreenWidth(), Game->ScreenHeight(),
- cbDoubleBuffer->GetSourceDC(), 0, 0, SRCCOPY);
-
- //release device context
- ReleaseDC(hWnd, hdc);
-
- }
-
- //////////////////////////////////////////////////////////////////////
- // StylusDown
- // Stylus press event
- //////////////////////////////////////////////////////////////////////
- void StylusDown(int x, int y)
- {
- //tap the screen to end program
- if (x > 45 && x < 195 && y > 275)
- Game->Shutdown();
- }
-
- //////////////////////////////////////////////////////////////////////
- // GameEnd
- // End of program event
- //////////////////////////////////////////////////////////////////////
- void GameEnd()
- {
- HWND hWnd = Game->GetWindow();
- HDC hdc = GetDC(hWnd);
-
- if (!SERVER_MODE)
- {
- shutdown(sock, 0);
- closesocket(sock);
- WSACleanup();
- }
-
- delete Game;
- delete cbDoubleBuffer;
- delete cbBackground;
- DeleteDC(hdc);
-
- }
-
- //////////////////////////////////////////////////////////////////////
- // StylusMove
- // Stylus drag across screen event
- //////////////////////////////////////////////////////////////////////
- void StylusMove(int x, int y)
- {
- }
-
- //////////////////////////////////////////////////////////////////////
- // StylusUp
- // Stylus release event
- //////////////////////////////////////////////////////////////////////
- void StylusUp(int x, int y)
- {
- }
-
-
- void ButtonPress(int iButtonID, POINT pt)
- {
- }
-
- void ButtonRelease(int iButtonID, POINT pt)
- {
- }
-
-
- //////////////////////////////////////////////////////////////////////
- // ServerThread
- //
- //////////////////////////////////////////////////////////////////////
- DWORD WINAPI ServerThread(LPVOID)
- {
- WSADATA wsaData;
- SOCKET sockConnected, sockListen;
- int nReceive, nSent;
- SOCKADDR_IN sockAddrClient;
- int nAddrLen = sizeof(SOCKADDR_IN);
- HDC hdc = cbBackground->GetSourceDC();
- xPos = 4;
- line = 5;
-
- if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0)
- {
- wsprintf(szSocketStatus, _T("Error initializing socket"));
- return 1;
- }
-
- //open server port
- sockListen = CreateListener();
- wsprintf(szSocketStatus, _T("Opened socket: %i"), sockListen);
- while (TRUE)
- {
- sockConnected = accept(sockListen, (LPSOCKADDR)&sockAddrClient, &nAddrLen);
- if (sockConnected == INVALID_SOCKET)
- {
- wsprintf(szSocketStatus, _T("Error accepting connection"));
- break;
- }
- else if (sockConnected != INVALID_SOCKET)
- {
- wsprintf(szSocketStatus, _T("Socket connected!"));
-
- while (TRUE)
- {
- nReceive = recv(sockConnected, szmbsBuffer, 1024, 0);
- if (nReceive == 0)
- {
- wsprintf(szSocketStatus, _T("Connection broken"));
- break;
- }
- else if (nReceive == SOCKET_ERROR)
- {
- wsprintf(szSocketStatus, _T("Error receiving data"));
- break;
- }
- else
- {
- wsprintf(szSocketStatus, _T("Receiving data..."));
-
- //convert to unicode
- szmbsBuffer[nReceive] = '\0';
- mbstowcs(szReceiveBuffer, szmbsBuffer, nReceive + 1);
- wcscpy(szIncomingMessage, szReceiveBuffer);
-
- bytesReceived += nReceive;
-
- //retrieve host name
- sockRet = gethostname(szHostName, 1024);
- sprintf(szBuffer, "Hello from %s!", szHostName);
- nToSend = strlen(szBuffer) + 1;
-
- //send text to other end
- nSent = send(sockConnected, szBuffer, nToSend, 0);
- if (nSent != SOCKET_ERROR)
- {
- wsprintf(szSocketStatus, _T("Sending data..."));
- }
-
- }
- }
-
- //connection broken
- shutdown(sockConnected, 0); //SD_BOTH);
- closesocket(sockConnected);
- bytesReceived = 0;
- }
- }
-
-
- //clean up winsock
- WSACleanup();
- return 0;
-
- }
-
- //////////////////////////////////////////////////////////////////////
- // CreateListener
- //
- //////////////////////////////////////////////////////////////////////
- SOCKET CreateListener()
- {
- SOCKADDR_IN sockAddrListen;
- SOCKET sockListen;
- DWORD address;
- char szHostName[1024];
- HOSTENT *lpHostEnt;
-
- //create a socket
- sockListen = socket(AF_INET, SOCK_STREAM, 0);
- if (sockListen == INVALID_SOCKET)
- return INVALID_SOCKET;
-
- if (gethostname(szHostName, 1024) == SOCKET_ERROR)
- return INVALID_SOCKET;
-
- lpHostEnt = gethostbyname(szHostName);
- if (lpHostEnt == NULL)
- return INVALID_SOCKET;
-
- memcpy(&address, lpHostEnt->h_addr_list[0], sizeof(address));
-
- memset(&sockAddrListen, 0, sizeof(SOCKADDR_IN));
-
- //set the port number
- sockAddrListen.sin_port = htons(SOCKET_PORT);
-
- //set address family
- sockAddrListen.sin_family = AF_INET;
-
- //set address for binding
- sockAddrListen.sin_addr.S_un.S_addr = address;
-
- //bind socket with SOCKADDR_IN
- if (bind(sockListen, (LPSOCKADDR)&sockAddrListen, sizeof(SOCKADDR)) == SOCKET_ERROR)
- return INVALID_SOCKET;
-
- //listen for connection
- if (listen(sockListen, 1) == SOCKET_ERROR)
- return INVALID_SOCKET;
-
- return sockListen;
-
- }
-
-
- void ClientConnect()
- {
- strcpy(szIPAddress, REMOTE_IP);
-
- //check the winsock library
- sockRet = WSAStartup(MAKEWORD(1,1), &wsaClientData);
- if (sockRet != 0)
- {
- wsprintf(szSocketStatus, _T("Winsock library not found!"));
- return;
- }
-
- //winsock will return version requested if valid
- if (LOBYTE(wsaClientData.wVersion) != 1 || HIBYTE(wsaClientData.wVersion) != 1)
- {
- wsprintf(szSocketStatus, _T("Winsock library version is invalid!"));
- WSACleanup();
- return;
- }
-
- //create socket
- sock = ConnectSocket(szIPAddress);
- if (sock == INVALID_SOCKET)
- {
- wsprintf(szSocketStatus, _T("Server not found"));
- WSACleanup();
- return;
- }
-
- wsprintf(szSocketStatus, _T("Connected"));
- bytesReceived;
- }
-
- //////////////////////////////////////////////////////////////////////
- // SocketClient
- //
- //////////////////////////////////////////////////////////////////////
- //void SocketClient()
- DWORD WINAPI ClientThread(LPVOID)
- {
- char szHostName[1024];
- char szBuffer[100];
- int nSent, nToSend, nReceive;
- int sockRet;
-
- //retrieve host name
- sockRet = gethostname(szHostName, 1024);
- sprintf(szBuffer, "Hello from %s!", szHostName);
- nToSend = strlen(szBuffer) + 1;
-
- ClientConnect();
- while (TRUE)
- {
- //send text to other end
- nSent = send(sock, szBuffer, nToSend, 0);
- if (nSent == SOCKET_ERROR)
- {
- wsprintf(szSocketStatus, _T("Connection broken"));
- //ClientConnect();
- break;
- }
- else
- wsprintf(szSocketStatus, _T("Sending data..."));
-
- nReceive = recv(sock, szmbsBuffer, 1024, 0);
- if (nReceive == 0)
- {
- wsprintf(szSocketStatus, _T("Connection broken"));
- break;
- //ClientConnect();
- }
- else if (nReceive == SOCKET_ERROR)
- {
- wsprintf(szSocketStatus, _T("Error receiving data"));
- break;
- }
- else
- {
- wsprintf(szSocketStatus, _T("Receiving data..."));
-
- //convert to unicode
- szmbsBuffer[nReceive] = '\0';
- mbstowcs(szReceiveBuffer, szmbsBuffer, nReceive + 1);
- wcscpy(szIncomingMessage, szReceiveBuffer);
-
- bytesReceived += nReceive;
- }
- }
- return 0;
- }
-
-
- //////////////////////////////////////////////////////////////////////
- // ConnectSocket
- //
- //////////////////////////////////////////////////////////////////////
- SOCKET ConnectSocket(char *szIPAddress)
- {
- DWORD dwDestAddr;
- SOCKADDR_IN sockAddrDest;
- SOCKET sockDest;
-
- //create socket
- sockDest = socket(AF_INET, SOCK_STREAM, 0);
- if (sockDest == SOCKET_ERROR)
- return INVALID_SOCKET;
-
- //convert address to socket form
- dwDestAddr = inet_addr(szIPAddress);
-
- //initialize IP address with port number
- memcpy(&sockAddrDest.sin_addr, &dwDestAddr, sizeof(DWORD));
- sockAddrDest.sin_port = htons(SOCKET_PORT);
- sockAddrDest.sin_family = AF_INET;
-
- //attempt connection
- if (connect(sockDest, (LPSOCKADDR)&sockAddrDest, sizeof(sockAddrDest)) == SOCKET_ERROR)
- {
- closesocket(sockDest);
- return INVALID_SOCKET;
- }
-
- return sockDest;
- }
-
-
-